-- BossStone
-- Created by dengc Sep/1/2016
-- boss层光/暗石柱

require "game/ui/form/stone/UIBossStone"

-- 初始化
function initBossStone(self)
    self.item:removeAllChildren();

    -- 石柱是否被关闭
    local stoneStatus = SaintTacticsM.getStoneStatus(self.gridData.pos);
    local isClosed = (stoneStatus == STONE_STATE_CLOSED) and true or false;

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            -- 更新建筑访问
            self:updateBuildingVisit();

            local pos = self.gridData:getPos();

            -- 打开石柱界面
            UIMgr.getCurrentScene():removeFormByName("UIBossStone");
            local uiForm = UIBossStone.create(pos);
            UIMgr.getCurrentScene():addForm(uiForm);

            -- 音效
            AudioM.playFx("ui_open1");
        end
    end

    local stoneId = self.gridData.stone_id;
    local stoneType = MagicStoneM.query(stoneId, "type");

    -- 用一张透明的图片来接收点击事件
    local iconName = MagicStoneM.getStoneIcon(stoneId);
    local iconPath = getLevelItemIconPath(iconName);
    self.icon = ccui.ImageView:create(iconPath);
    self.item:addChild(self.icon);
    self.icon:setOpacity(0);
    self.icon:addTouchEventListener(onClicked);

    -- 初始化不可点击
    self.icon:setTouchEnabled(false);

    -- 创建石柱模型，初始停在第一帧
    local effectId = iif(stoneType == STONE_TYPE_LIGHT, 1197, 1192);
    local animation = createEffectAnim(effectId);
    animation:gotoFrameAndPause(0);
    self.item.animation = animation;
    self.item:addChild(animation);

    self.item:setPositionY(10);
 end

 -- 石柱关闭
function onCloseBossStone(self)
    -- 播放石柱关闭特效
    playStoneCloseEffect(self);

    -- 设置为不可点击
    self.icon:setTouchEnabled(false);
end

-- 石柱开启
function onOpenBossStone(self)
    -- 播放石柱禁魔特效
    playForbidMagicEffect(self);

    -- 设置为可点击状态
    self.icon:setTouchEnabled(true);
end

-- 石柱消失
function onStoneDisappear(self)
    -- 移除石柱警戒
    AlertStateM.clearState(AlertStateM.ALERT_STATE_LIGHT_STONE);
    AlertStateM.clearState(AlertStateM.ALERT_STATE_DARK_STONE);

    local function disappear()
        playStoneDisappear(self);
    end

    -- 播放石柱消失特效
    performWithDelay(self, disappear, 0.5);
end
